/**
 * 页面服务
 */
package cn.antia.cep.busi;

import java.util.*;

import cn.antia.cep.common.*;
import cn.antia.cep.common.pool.*;
import cn.antia.cwin.DBConfiger;
import cn.antia.cwin.NormalTD;
import cn.antia.cwin.Cele;
import cn.antia.cep.page.*;

/**
 * @author sunkeei
 * @version 0.1
 * @date Oct 8, 2006
 */
public class PageService {
	private AntiaPoolHelper antiaPoolHelper;
	public AntiaPoolHelper getAntiaPoolHelper() {
		return antiaPoolHelper;
	}
	public void setAntiaPoolHelper(AntiaPoolHelper antiaPoolHelper) {
		this.antiaPoolHelper = antiaPoolHelper;
	}
	/**
	 * 取得FORM对象
	 * @param comsats:FORM的各元素
	 * @param table:表名
     * @return FORM对象
     * @throws cn.antia.cwin.ConfigException config*/
	public AntiaForm getAntiaForm(List comsats,String table)  {
		AntiaForm form = new AntiaForm();
		//默认规则
		form.setAction("dataOperateAction.do");
		form.setMethod("post");
		form.setFormName(PageRule.getFormName(table));
		
		Cele ec = new Cele();
		Object[] ecs = comsats.toArray();
		//为这些元素排序，以便于在页面上显示
		Arrays.sort(ecs,ec);
		comsats = new ArrayList();
		for(int i = 0;i<ecs.length;i++){
			comsats.add(ecs[i]);
		}
		form.setFormList(comsats);
		form.setRowNum(Integer.parseInt(DBConfiger.getDispColumn(table)));
		return form;
	}
	
	/**
     * 取得显示表格的数据
     *
     * @param table 表名
     * @param rows 行数据,每一行一个List对象
     * @return 表格
     * @throws cn.antia.cwin.ConfigException config*/
    public AntiaTable getAntiaTable(List rows,String table){
		AntiaTable at = new AntiaTable();
		//设置表格ID
		at.setTableId(PageRule.getTableId(table));
		//
		//设置表头,表头LIST为ValueDisp对象的集合
		//其中ValueDisp中ID存入value中，显示表头存入disptext中
		List th = new ArrayList();
		if(rows == null || rows.size() < 1){
			at.setRows(null);
			at.setTh(null);
			return at;
		}
		List row = (List)rows.get(0);
		ValueDisp vd = new ValueDisp();
		Cele ec,comsat;
        for (Iterator it = row.iterator(); it.hasNext();) {
            Object aRow = it.next();
            ec = (Cele) aRow;
            String field = ec.getField().getFieldName();
            vd.setName(PageRule.getTdId(table, field));
            comsat = antiaPoolHelper.getElementComsat(table, ec.getField().getFieldName());
            vd.setDispText(comsat.getHtmlWidget().getDispText());
            th.add(vd);
            vd = new ValueDisp();
        }
        at.setTh(th);
		//设置其它数据行,每一行数据以字符数组方式存储
		List tbody = new ArrayList();
        for (Iterator it1 = rows.iterator(); it1.hasNext();) {
        	row = (List)it1.next();
            NormalTD[] values = new NormalTD[row.size()];
            for (int j = 0; j < row.size(); j++) {
                ec = (Cele) row.get(j);
                comsat = antiaPoolHelper.getElementComsat(table, ec.getField().getFieldName());
                NormalTD td = new NormalTD();
                //td.setValue(ec.getValue().toString());
                if(comsat.getTd() != null){
                	td.setDispMode(comsat.getTd().getDispMode());
                }
                values[j] = td;
            }
            tbody.add(values);
        }
        at.setRows(tbody);
		return at;
	}
}